﻿using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Limestone.Fields;
using Utils;

namespace Limestone
{

    [Serializable]
    [XmlRoot("Page", Namespace = "http://www.limestone.se", IsNullable = false)]
    public class Page :IPage
    {
        #region props and fields

        [XmlAttribute]
        public string Name { get; set; }
        /// <summary>
        /// The path always begins with a / and includes the page name
        /// </summary>
        [XmlAttribute]
        public string Path { get; set; }
        [XmlAttribute]
        public string Title { get; set; }
        [XmlAttribute]
        public string TemplatePath { get; set; }
        [XmlAttribute]
        public bool Hidden { get; set; }
        [XmlAttribute]
        public PageSortType SortChildPagesBy { get; set; }
        [XmlAttribute]
        public int SortValue { get; set; }
        [XmlArray("Fields")]
        [XmlArrayItem("Field", Type = typeof(AbstractXmlSerializer<Field>))]
        public FieldCollection Fields { get; set; }
        [XmlIgnore]
        public string ParentPath
        {
            get {
                return PageHelper.GetParentPath(Path);
            }
        }
        
        [XmlAttribute]
        public DateTime Created { get; set; }
        [XmlAttribute]
        public string CreatedBy { get; set; }
        [XmlAttribute]
        public DateTime Modified { get; set; }
        [XmlAttribute]
        public string ModifiedBy { get; set; }
        [XmlAttribute]
        public DateTime Deleted { get; set; }
        [XmlAttribute]
        public string DeletedBy { get; set; }

        #endregion

        public Page()
        {
            Fields = new FieldCollection {Page = this};
            Created = new DateTime();
            CreatedBy = string.Empty;
            SortValue = -1;
            SortChildPagesBy = PageSortType.Title;
            TemplatePath = string.Empty;
            Title = "New page";
            Name = ToFriendlyUrl("New page");
            Path = "/" + Name;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        public Page(string title)
        {
            Fields = new FieldCollection {Page = this};
            Created = new DateTime();
            CreatedBy = string.Empty;
            Hidden = false;
            Title = title;
            Name = ToFriendlyUrl(title);
            Path = "/" + Name;
            SortValue = -1;
            SortChildPagesBy = PageSortType.Title;
            TemplatePath = string.Empty;
        }

        

        #region Tools


        private string ToFriendlyUrl(string title)
        {
            var name = new StringBuilder();

            foreach (var c in Title)
            {
                if (c.ToString(CultureInfo.InvariantCulture) == " ")
                    name.Append("-");
                else
                {
                    if ("?&:/!#@$§{[]}".Contains(c))
                        continue;
                    name.Append(c.ToString(CultureInfo.InvariantCulture));
                }
            }

            // TODO unique name in this directory

            return name.ToString();
        }
        #endregion
    }
}
